import { defineStore } from 'pinia'

export const useBookStore = defineStore('book', {
  state: () => ({
    books: [],
    borrowedBooks: [],
    loading: false,
    error: null
  }),
  
  actions: {
    async fetchBooks() {
      this.loading = true
      try {
        // 这里后续会添加实际的API调用
        this.books = [
          { id: 1, title: 'Vue.js 实战', author: '梁灏', publisher: '电子工业出版社', status: '可借阅' },
          { id: 2, title: 'JavaScript 高级程序设计', author: 'Nicholas C. Zakas', publisher: '人民邮电出版社', status: '可借阅' },
          { id: 3, title: '深入浅出 Node.js', author: '朴灵', publisher: '人民邮电出版社', status: '借阅中' }
        ]
      } catch (error) {
        this.error = error.message
      } finally {
        this.loading = false
      }
    },
    
    async borrowBook(bookId) {
      try {
        // 这里后续会添加实际的API调用
        const book = this.books.find(b => b.id === bookId)
        if (book) {
          book.status = '借阅中'
          this.borrowedBooks.push(book)
        }
      } catch (error) {
        this.error = error.message
      }
    },
    
    async returnBook(bookId) {
      try {
        // 这里后续会添加实际的API调用
        const book = this.books.find(b => b.id === bookId)
        if (book) {
          book.status = '可借阅'
          this.borrowedBooks = this.borrowedBooks.filter(b => b.id !== bookId)
        }
      } catch (error) {
        this.error = error.message
      }
    }
  }
}) 